package com.rickpan.repository;

import com.rickpan.entity.FriendGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 好友分组Repository
 */
@Repository
public interface FriendGroupRepository extends JpaRepository<FriendGroup, Long> {

    /**
     * 根据用户ID查找所有分组
     */
    List<FriendGroup> findByUserIdOrderBySortOrderAscCreatedAtAsc(Long userId);

    /**
     * 根据用户ID和分组名称查找分组
     */
    Optional<FriendGroup> findByUserIdAndGroupName(Long userId, String groupName);

    /**
     * 检查分组名称是否已存在
     */
    boolean existsByUserIdAndGroupName(Long userId, String groupName);

    /**
     * 获取用户的分组名称列表
     */
    @Query("SELECT fg.groupName FROM FriendGroup fg WHERE fg.userId = :userId ORDER BY fg.sortOrder ASC, fg.createdAt ASC")
    List<String> findGroupNamesByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID删除所有分组
     */
    void deleteByUserId(Long userId);

    /**
     * 获取用户分组数量
     */
    long countByUserId(Long userId);
}
